package easy;

public class Solution_190 {

    public static int reverseBits(int n) {
        boolean flag = n>=0;
        int[] ori;
        if (!flag) {
            ori = new int[]{1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1};
            n = 0 - n -1;
        } else {
            ori = new int[32];
        }
//        n = Math.abs(n);
        int res = 0,mod;
        for (int i=31;;i--) {
            mod = n%2;
            n /= 2;
            if (mod==1) {
                if (flag) {
                    ori[i] = 1;
                } else {
                    ori[i] = 0;
                }
            }
            if (n==0)
                break;
        }
        flag = ori[31]==0;
        for (int i = 30; i >= 0; i--) {
            res = res*2 + (flag?ori[i]:1-ori[i]);
        }
//        res = res*2;

        StringBuffer b = new StringBuffer();
        for (int i : ori) {
            b.append(i);
        }
        System.out.println("原："+b);
        System.out.println("新："+b.reverse());
        return flag?res:0-res-1;
    }

    public static void main(String[] args) {
        int a = 964176192;
        int i = reverseBits(-3);
        System.out.println(i);
    }
}
